package org.springblade.application.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.shy.core.sso.common.dto.RDto;
import org.shy.core.sso.common.util.StringUtil;
import org.springblade.application.service.AppService;
import org.springblade.application.wrapper.AppWrapper;
import org.springblade.applicationapi.entity.App;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;


/**
 * description: AppController 第三方应用相关接口 <br>
 * date: 2022/4/6 9:27 <br>
 * author: yzpang <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("app")
@Slf4j
@Data
public class AppController {
    private final AppService appService;

    /**
     * description:  添加应用<br>
     * date: 2022/4/6 9:55 <br>
     * author: yzpang <br>
     * param:
     * @return
    **/
    @PostMapping("add")
    public R<App> addApp(@RequestBody App app){
        App app1 = appService.getAppByAppName(app.getAppName());

        if (app1 != null){
            return R.fail("应用["+ app.getAppName() +"]已存在");
        }
		app.setCreateTime(new Timestamp(System.currentTimeMillis()));
		if (StringUtils.isEmpty(app.getAppId())){
			String appid = StringUtil.generateUUIDString(16);
			app.setAppId(appid);
		}
		if (StringUtils.isEmpty(app.getAppSecret())){
			String secret = StringUtil.generateUUIDString(48);
			app.setAppSecret(secret);
		}
		if (StringUtils.isEmpty(app.getToken())){
			String token = StringUtil.generateUUIDString(8);
			app.setToken(token);
		}
		if (StringUtils.isEmpty(app.getAccessAesKey())){
			String accessAESKey = StringUtil.generateUUIDString(43);
			app.setAccessAesKey(accessAESKey);
		}
        appService.addApp(app);
        return R.data(app);
    }

    /**
     * description:刷新appsecret <br>
     * date: 2022/4/6 9:55 <br>
     * author: yzpang <br>
     * param:
     * @return
    **/
    @PostMapping("refresh")
    public R<App> refreshSecret(@RequestParam("appid") String appid){
        App app = appService.getAppByAppId(appid);
        if (app == null){
            RDto.fail("appid"+appid+"不存在");
        }
        String appsecret = StringUtil.generateUUIDString(48);
        app.setAppSecret(appsecret);
        appService.updateApp(app);
        return R.data(app);
    }

    @PostMapping("update")
    public R<App> updateApp(@RequestBody App app){
		app.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        App app1 = appService.updateApp(app);
        return R.data(app1);
    }

	@PostMapping("remove")
	public R deleteApp(String ids){
		appService.deleteById(ids);
		return R.success("ok");
	}

	@PostMapping("list")
	public R<List<App>> findAll(){
		List<App> appList =  appService.findAppList();
		return R.data(appList);
	}

	@PostMapping("query")
	public R<IPage<App>> query(Query query, App app, String order, String name){
		log.info("排序方式{}", order);
		return R.data(appService.selectAppPage(Condition.getPage(query), app, order, name));
	}

	@GetMapping("page")
	public R<IPage<App>> page(Query query){

		IPage<App> pages = appService.selectAppPage(Condition.getPage(query));
		return R.data(AppWrapper.build().pageVO(pages));
	}

	@GetMapping("secret")
	public R<Map<String, String>> secret(){
		String appid = StringUtil.generateUUIDString(16);
		String appsecret = StringUtil.generateUUIDString(48);
		Map<String, String> map = new HashMap<>();
		map.put("appId", appid);
		map.put("appSecret", appsecret);
		return R.data(map);
	}
}
